/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.cell.value.salary.enhance;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Collections;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;

import com.reportgear.core.data.SalaryHelper;
import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.core.swing.pane.base.JBasePane;
import com.reportgear.report.core.api.idname.DefaultIdName;
import com.reportgear.report.core.api.idname.IdName;

/**
 * 工资主对象排序面板
 * 
 * @version 1.0 2011-6-13
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class SalaryMainObjectSortPane extends JBasePane {
	// 排序类型
	private JComboBox orderTypeCbx;
	private JList objectList;
	private DefaultListModel listModel = new DefaultListModel();

	@SuppressWarnings("unchecked")
	public SalaryMainObjectSortPane() {
		this.setLayout(new BorderLayout());

		orderTypeCbx = new JComboBox(new Object[] { OrderObject.Organization, OrderObject.CostCenter });
		this.add(orderTypeCbx, BorderLayout.NORTH);
		orderTypeCbx.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent evt) {
				JComboBox cbx = (JComboBox) evt.getSource();

				if (evt.getStateChange() == ItemEvent.SELECTED) {
					loadOrderInfo((IdName<String>) cbx.getSelectedItem());
				}
			}
		});

		JPanel centerPane = new JPanel();
		centerPane.setLayout(new BorderLayout());
		this.add(centerPane, BorderLayout.CENTER);

		objectList = new JList(listModel);
		objectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		centerPane.add(new JScrollPane(objectList), BorderLayout.CENTER);

		JPanel btnPane = new JPanel();
		Box verticalBox = Box.createVerticalBox();
		btnPane.add(verticalBox);

		verticalBox.add(Box.createVerticalStrut(100));

		JButton btnSave = new JButton("保存");
		btnSave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				IdName<String> selectedItem = (IdName<String>) orderTypeCbx.getSelectedItem();
				if (selectedItem == null) {
					return;
				}

				saveOrderInfo();
			}
		});
		verticalBox.add(btnSave);

		verticalBox.add(Box.createVerticalStrut(20));

		JButton btnRefresh = new JButton("刷新");
		btnRefresh.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {

				IdName<String> selectedItem = (IdName<String>) orderTypeCbx.getSelectedItem();
				if (selectedItem == null) {
					return;
				}

				refreshOrderInfo((IdName<String>) orderTypeCbx.getSelectedItem());
			}
		});
		verticalBox.add(btnRefresh);

		verticalBox.add(Box.createVerticalStrut(20));

		JButton btnUP = new JButton("向上");
		btnUP.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				int index = objectList.getSelectedIndex();

				if (index == -1) {
					return;
				}

				if (index == 0) {
					return;
				}
				Object o = listModel.remove(index);
				listModel.add(index - 1, o);
				objectList.setSelectedIndex(index - 1);
			}
		});

		verticalBox.add(btnUP);

		verticalBox.add(Box.createVerticalStrut(20));

		JButton btnDOWN = new JButton("向下");
		verticalBox.add(btnDOWN);
		btnDOWN.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				int index = objectList.getSelectedIndex();
				if (index == -1) {
					return;
				}

				if (index == listModel.size() - 1) {
					return;
				}

				Object o = listModel.remove(index);
				listModel.add(index + 1, o);
				objectList.setSelectedIndex(index + 1);
			}
		});

		centerPane.add(btnPane, BorderLayout.EAST);

		this.loadOrderInfo((IdName<String>) orderTypeCbx.getSelectedItem());
	}

	/**
	 * 加载排序信息
	 * 
	 * @param selectedItem
	 *            选择项
	 */
	private void loadOrderInfo(IdName<String> selectedItem) {
		if (selectedItem == null) {
			return;
		}

		List<SalaryDataObject> items = SalaryHelper.INSTANCE.getOrderInfo(selectedItem.getId());
		listModel.clear();
		for (SalaryDataObject sdo : items) {
			listModel.addElement(new DefaultIdName<String>(sdo.getString("id"), sdo.getString("name")));
		}

		if (listModel.size() > 0) {
			objectList.setSelectedIndex(0);
		}
	}

	/**
	 * 重新刷新
	 * 
	 * @param selectedItem
	 */
	private void refreshOrderInfo(IdName<String> selectedItem) {
		String id = selectedItem.getId();

		List<SalaryDataObject> items = Collections.emptyList();
		if (OrderObject.ORDER_KEY_ORGANIZATION.equals(id)) {
			items = SalaryHelper.INSTANCE.getOrgs();
		} else if (OrderObject.ORDER_KEY_COSTCENTER.equals(id)) {
			items = SalaryHelper.INSTANCE.getCenters();
		}
		// 以后添加其它的

		listModel.clear();
		for (SalaryDataObject sdo : items) {
			listModel.addElement(new DefaultIdName<String>(sdo.getString("id"), sdo.getString("name")));
		}

		if (listModel.size() > 0) {
			objectList.setSelectedIndex(0);
		}
	}

	@SuppressWarnings("unchecked")
	private void saveOrderInfo() {
		StringBuilder sb = new StringBuilder("<root>");
		for (Object o : listModel.toArray()) {
			IdName<String> idName = (IdName<String>) o;
			sb.append(String.format("<item id=\"%s\" name=\"%s\"/>", idName.getId(), idName.getName()));
		}

		sb.append("</root>");

		int result = SalaryHelper.INSTANCE.saveOrderInfo(
				((IdName<String>) this.orderTypeCbx.getSelectedItem()).getId(), sb.toString());
	}

	public static void main(String[] args) {
		SalaryMainObjectSortPane pane = new SalaryMainObjectSortPane();

		pane.showDialog(null, "排序", 350, 600);
	}
}
